package com.mellarius.Adaptor;

import java.util.List;

import com.mellarius.R;
import com.model.Agenda;
import com.opration.Control;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class AgendaAdaptor extends ArrayAdapter<Agenda> {

	Context context;

	public AgendaAdaptor(Context context, List<Agenda> ts) {
		super(context, R.layout.listview_agenda, ts);
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Agenda agendaHelper = (Agenda) this.getItem(position);
		TextView tarefa;
		TextView apiario;
		CheckBox cb;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.fragment_agenda, parent, false);
			tarefa = (TextView) convertView.findViewById(R.id.agendaActivity);
			apiario = (TextView) convertView.findViewById(R.id.agendaApiario);
			cb = (CheckBox) convertView.findViewById(R.id.agendaConcluido);
			convertView.setTag(new AgendaViewHolder(tarefa, apiario, cb));
			cb.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {

					CheckBox cb = (CheckBox) v;
					Agenda hive = (Agenda) cb.getTag();
					if (Control.getDate(hive.getDatanormal()).equals(Control.getDate())) {
						if (cb.isChecked()) {
							hive.setConcluido(true);
							Control.updateAgenda(hive);
							notifyDataSetChanged();
						} else {
							hive.setConcluido(false);
							Control.updateAgenda(hive);
							notifyDataSetChanged();
						}
					}else{
						cb.setChecked(!cb.isChecked());
					}
					
				}

			});
		} else {
			// Because we use a ViewHolder, we avoid having to call
			// findViewById().
			AgendaViewHolder viewHolder = (AgendaViewHolder) convertView.getTag();
			cb = viewHolder.getCheckBox();
			tarefa = viewHolder.getActivity();
			apiario = viewHolder.getApiario();

		}
		// Tag the CheckBox with the Hive it is displaying, so that we can
		// access the hive in onClick() when the CheckBox is toggled.
		cb.setTag(agendaHelper);
		// Display hive data
		tarefa.setText(agendaHelper.getTarefa());
		apiario.setText(agendaHelper.getApiario().getIdentificacao());
		cb.setChecked(agendaHelper.getConcluido());

		return convertView;
	}

	/** Holds child views for one row. */
	private static class AgendaViewHolder {

		public TextView activity;
		public TextView apiario;
		public CheckBox estado;

		public AgendaViewHolder(TextView activity, TextView api, CheckBox ck) {

			this.activity = activity;
			this.apiario = api;
			this.estado = ck;
		}

		public TextView getApiario() {
			return apiario;
		}

		public TextView getActivity() {
			return activity;
		}

		public CheckBox getCheckBox() {
			return estado;
		}

	}
}
